package com.itheima.service.impl;

import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.GetOrredingIdUUID;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private OrderMapper orderMapper;

    //添加套餐订单
    @Override
    public Order addOrder(Map map) {
        Order order=new Order();
        //判断用户是否为第一次预约，获取用户的手机号，在会员表t_member表中查找用户信息是否存在
        String telephone = (String) map.get("telephone");
        Member member= memberMapper.findMemberByPhone(telephone);

        //根据预约时间判断是否可以预约RESERVATIONS
        String orderDate = (String) map.get("orderDate");
        int count= orderSettingMapper.findReservationsByOrderData(orderDate);

        try {
            Date date=DateUtils.parseString2Date(orderDate);
            order.setOrderDate(date);
            order.setOrderStatus("0");
            order.setOrderType((String) map.get("orderType"));
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
            order.setOrderId(GetOrredingIdUUID.getOrderIdByUUId());
            order.setPayStatus(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //如果时第一次预约，收集用户信息
        if (member==null){
            Member newMember=new Member();
            newMember.setName((String) map.get("name"));
            newMember.setIdCard((String) map.get("idCard"));
            newMember.setPhoneNumber(telephone);
            newMember.setRegTime(new Date());
            newMember.setSex((String) map.get("sex"));
            memberMapper.addMember(newMember);
            if (count>0){ //预约数未满，可以直接生成订单
                order.setMemberId(newMember.getId());
                orderMapper.addOrder(order);
                //t_ordersetting表的预约数加一
                orderSettingMapper.updateReservationsByDate(orderDate);

            }else {//预约数已满
                throw new RuntimeException("预约数已满，请重新选择预约日期");
            }
            //将
        }else {//不是第一次预约，判断预约数是否已满
            order.setMemberId(member.getId());
            if (count>0){
                //判断同一个用户是否预约了同一天的同一个套餐
              Integer num=orderMapper.findOrderAndOrderSettingByDateAndMemberId(order);
              if (num>0){ //会员已经预约过
                  throw new RuntimeException("您已预约，请选择其他时间");
              }else { //会员没有预约

                  orderMapper.addOrder(order);
                  //t_ordersetting表的预约数加一
                  orderSettingMapper.updateReservationsByDate(orderDate);
              }
            }else {
                throw new RuntimeException("预约数已满，请重新选择预约日期");
            }
        }


        return order;
    }


    //查询预约详情
    @Override
    public Map findOrderDetail(Integer id) {
        Map map=orderMapper.findOrderDetail(id);
        return map;
    }

    @Override
    public String findOrderIdById(Integer oid) {
        String orderId = orderMapper.findOrderIdById(oid);
        return orderId;
    }

    @Override
    public void updateState(Map<String,String> param) {
        // 1.获取订单号
        String orderId = param.get("out_trade_no");
        System.out.println("订单号:"+orderId);

        // 2.修改订单状态
        orderMapper.updateState(orderId);



    }

    @Override
    public Order findStatus(Integer oid) {
        Order order=orderMapper.findStatus(oid);
        return order;
    }
}
